package com.geek.framework.study.aquickstart.netflix;

import com.geek.framework.study.config.netflix.SimplePolledConfigurationSource;
import com.netflix.config.*;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static com.geek.framework.study.config.netflix.SimplePolledConfigurationSource.KEY;


public class PollDynamicPropertiesDemo {

    public static void main(String[] args) throws InterruptedException {
        PolledConfigurationSource source = new SimplePolledConfigurationSource();
        AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(1000, 5000, false);
        ConfigurationManager.install(new DynamicConfiguration(source, scheduler));

        DynamicLongProperty prop = DynamicPropertyFactory.getInstance().getLongProperty(KEY, 0L);

        AtomicLong atomicLong = new AtomicLong(prop.getValue());
        System.out.println("atomicLong = " + atomicLong);

        prop.addCallback(()->{
            Long oldValue = atomicLong.get();
            Long newValue = prop.getValue();

            atomicLong.set(newValue);
            System.out.printf("[线程：%s] oldValue = %d, newValue = %d \n",
                    Thread.currentThread().getName(),
                    oldValue,
                    newValue
            );
        });
        TimeUnit.MINUTES.sleep(1);
    }
}
